package com.digitalchina.model.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.digitalchina.industry.dao.EyeModelIndustryMapper;
import com.digitalchina.industry.model.EyeModelIndustry;
import com.digitalchina.model.dao.EyeModelDataMapper;
import com.digitalchina.model.model.EyeModelData;
import com.digitalchina.user.dao.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang_ on 2017/7/11.
 */
@Service
public class ModelService {

    @Autowired
    private EyeModelDataMapper eyeModelDataMapper;
    @Autowired
    private EyeModelIndustryMapper eyeModelIndustryMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    public int findTotalCount(Map<String, Object> params) {
        return eyeModelDataMapper.getCount(params);
    }

    public List<EyeModelData> findAll(Map<String, Object> params) {
        return eyeModelDataMapper.getEyeModelData(params);
    }
    public EyeModelData findById(String Id) {
        EyeModelData t = new EyeModelData();
        t.setId(Integer.valueOf(Id));
        EyeModelData eyeModel = eyeModelDataMapper.selectOne(t);
        /*if(eyeModel.getModelIndustry()!=null && !eyeModel.getModelIndustry().equals("")){
            List intrestIndus = new ArrayList();
            String[] intrestIndusId = eyeModel.getModelIndustry().split(",");
            for(int i = 0; i<intrestIndusId.length;i++){
                Map map = new HashMap();
                map.put("id",intrestIndusId[i]);
                map.put("text",changeInduName(Integer.valueOf(intrestIndusId[i])));
                intrestIndus.add(map);
            }
            eyeModel.setModelIndustry(JSON.toJSONString(intrestIndus) );
        }*/
        List intrestIndus = new ArrayList();
        List<String> listIndustry = eyeModelIndustryMapper.findIndustryId(eyeModel.getId().toString());
        for(int i = 0; i<listIndustry.size();i++){
            Map map = new HashMap();
            map.put("id",listIndustry.get(i));
            map.put("text",changeInduName(Integer.valueOf(listIndustry.get(i))));
            intrestIndus.add(map);
        }

        eyeModel.setModelIndustry(JSON.toJSONString(intrestIndus) );


        /*if(eyeModel.getModelScene()!=null && !eyeModel.getModelScene().equals("")){
            List intrestScene = new ArrayList();
            String[] intrestSceneId = eyeModel.getModelScene().split(",");
            for(int i = 0; i<intrestSceneId.length;i++){
                Map map = new HashMap();
                map.put("id",intrestSceneId[i]);
                map.put("text",changeSceName(Integer.valueOf(intrestSceneId[i])));
                intrestScene.add(map);
            }
            eyeModel.setModelScene(JSON.toJSONString(intrestScene) );
        }*/
        List intrestScene = new ArrayList();
        List<String> intrestSceneId = eyeModelIndustryMapper.findSceneId(eyeModel.getId().toString());
        for(int i = 0; i<intrestSceneId.size();i++){
            Map map = new HashMap();
            map.put("id",intrestSceneId.get(i));
            map.put("text",changeSceName(Integer.valueOf(intrestSceneId.get(i))));
            intrestScene.add(map);
        }
        eyeModel.setModelScene(JSON.toJSONString(intrestScene) );

        return eyeModel;
    }

    private Object changeSceName(Integer id) {
        String name =userInfoMapper.getSceneName(id);
        if(null!=name && !("".equals(name))){
            return name;
        }else{
            return "";
        }
    }

    private String changeInduName(Integer id) {
        String name =userInfoMapper.getName(id);
        if(null!=name && !("".equals(name))){
            return name;
        }else{
            return "";
        }

    }

    public int updata(EyeModelData eyeModelData){
        return eyeModelDataMapper.updateByPrimaryKeySelective(eyeModelData);
    }

//    public String getEyeModelIndustry(String industryId){
//        StringBuffer sb = new StringBuffer();
//        List<String> list = eyeModelIndustryMapper.findName(industryId.split(","));
//        for (String name:list){
//            sb.append(name);
//            sb.append(",");
//        }
//        return sb.toString();
//    }
    //获取模型的行业
    public String getEyeModelIndustry(String modelId){
        StringBuffer sb = new StringBuffer();
        List<String> list = eyeModelIndustryMapper.findName(modelId);
        for (String name:list){
            sb.append(name);
            sb.append(",");
        }
        return sb.toString();
    }
    //获取模型的使用场景
    public String getEyeModelScene(String modelId){
        StringBuffer sb = new StringBuffer();
        List<String> list = eyeModelIndustryMapper.findSceneName(modelId);
        for (String name:list){
            sb.append(name);
            sb.append(",");
        }
        return sb.toString();
    }
    //模型审核时的模型信息
    public Map<String,String> getModelExamineInfo(String modelId){
        Map<String,String> map = eyeModelDataMapper.getModelExamineInfo(modelId);
        return map;
    }
}
